
var path = require('path');
var fs = require('fs');
module.exports = class extends think.Controller {
  async signinAction() {
    const is_login = await this.islogin();
    if (this.isAjax() && this.get('redirect') != '1') {
      if (Number(this.config('ext.geetest.isa')) === 1) {
        const geetest = this.service('ext/geetest');
        const res = await geetest.validate(this.ctx, this.post());
        console.log(res.status);
        if (res.status !== 'success') {
          const error = this.controller('kms/error');
          return error.noAction('验证码不正确');
        }
      }

      const username = this.post('username');
      let password = this.post('password');
      password = encryptPassword(password);
      const res = await this.model('kms/member').signin(username, password, this.ip, 1, 1);
      if (res.uid > 0) {
        await this.session('userInfo', res);
        return this.success({name: '登录成功!', url: '/admin/index'});
      } else {
        console.log(res);
        let fail;
        switch (res) {
          case -1: 
            fail = '用户不存在或被禁用';
            break;
          case -2:
            fail = '密码错误';
            break;
          case -3:
            fail = '您无权登录后台';
            break;
          default:
            fail = '未知错误';
        }
        const error = this.controller('kms/error');
        return error.noAction(fail);
      }
    } else {
      if (is_login) {
        this.redirect('/admin/index');
      } else {
        return this.display();
      }
    }
  }

  async loginAction() {
    const is_login = await this.islogin();
    if (this.isAjax()) {
      if (Number(this.config('ext.geetest.isa')) === 1) {
        const geetest = this.service('ext/geetest');
        const res = await geetest.validate(this.ctx, this.post());
        console.log(res.status);
        if (res.status !== 'success') {
          const error = this.controller('kms/error');
          return error.noAction('验证码不正确');
        }
      }

      const username = this.post('username');
      let password = this.post('password');
      password = encryptPassword(password);
      const res = await this.model('kms/member').signin(username, password, this.ip, 1, 1);
      if (res.uid > 0) {
        await this.session('userInfo', res);
        return this.success({name: '登录成功!', url: '/admin/index'});
      } else {
        console.log(res);
        let fail;
        switch (res) {
          case -1: 
            fail = '用户不存在或被禁用';
            break;
          case -2:
            fail = '密码错误';
            break;
          case -3:
            fail = '您无权登录后台';
            break;
          default:
            fail = '未知错误';
        }
        const error = this.controller('kms/error');
        return error.noAction(fail);
      }
    } else {
      if (is_login) {
        this.redirect('/admin/index');
      } else {
        return this.display();
      }
    }
  }

  async islogin() {
    const user = await this.session('userInfo');
    const res = !think.isEmpty(user);
    return res;
  }

  async outAction() {
    // 退出登录
    const is_login = await this.islogin();
    if (is_login) {
      await this.session('userInfo', null);
      this.redirect('/admin/public/signin');
    } else {
      this.redirect('/admin/public/signin?redirect=1');
    }
  }

  async uploadAction() {
    const file = think.extend({}, this.file('file'));
    console.log(file)
    // console.log(file);
    const filepath = file.path;
    const extname = path.extname(file.name);
    const basename = path.basename(filepath) + extname;
    let res;
    let pic_path;
    // 强势插入七牛
    const uploadPath = think.resource + '/upload/picture/' + dateformat('Y-m-d', new Date().getTime());
      think.mkdir(uploadPath);
      if (think.isFile(filepath)) {
        fs.renameSync(filepath, uploadPath + '/' + basename);
      } else {
        console.log('文件不存在！');
      }
      file.path = uploadPath + '/' + basename;
      pic_path = '/upload/picture/' + dateformat('Y-m-d', new Date().getTime()) + '/' + basename;
      if (think.isFile(file.path)) {
        const data = {
          path: pic_path,
          create_time: new Date().getTime(),
          status: 1
        };
        res = await this.model('picture').add(data);
      } else {
        console.log('not exist');
      }

    this.json({
      code: 'SUCCESS',
      data: {
        path: pic_path,
        url: pic_path
      }
    });
  }
}